package com.store.app.activity;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.store.app.BaseActivity;
import com.store.app.R;
import com.store.app.adapter.XiaoPiaoAdapter;
import com.store.app.bean.GoodsListBean;
import com.store.app.http.Stores;
import com.store.app.utils.MoneyUtil;
import com.store.app.utils.SettingData;
import com.store.app.utils.Utils;

import java.util.List;

/**
 * Created by Administrator on 2016/11/3.
 * 小票页面
 */
public class XiaopiaoDialogActivity extends BaseActivity{
    private String price_original = "0";            //应付金额

    private String needPay = "0";                  //应付－折扣 = 应收
    private TextView store_name;//店铺名
    private TextView order_no;//单号
    private TextView order_date;//时间
    private TextView zhongduan;//终端
    private ListView listview;
    private TextView listview_totalnum;
    private TextView xp_hyyh_jm;//减免
    private TextView xp_hyyh_fx;//返现
    private TextView xp_hyyh_sb;//送币
    private TextView xp_qtdk;//其他抵扣
    private TextView xp_psfy;//配送费用
    private TextView xp_zffs;//支付方式
    private TextView xp_sjfk;//实际付款
    private TextView xp_gmcp;//购买彩票
    private TextView xp_sjsk_bottom;//实际收款
    private LinearLayout lin_bottom_dy;//底部打印
    private LinearLayout lin_bottom_cp;//底部彩票
    private LinearLayout lin_bottom_yhq;//底部优惠券
    private ImageView img_bottom_close;//底部关闭
    private String  pay_type;//支付方式
    private String change="0.00";//零钱找零
    private String dikou="0.00";//抵扣
    private String fangxian="0.00";//返现
    private String songbi="0";//送币
    private String discount_price = "0.00";//减免
    private String s_outtradeno_date="";//交易时间
    private String s_outtradeno="";//交易单号

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_xiaopiao);
        getWindow().setLayout(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT);
        price_original=getIntent().getStringExtra("price_original");
        discount_price=getIntent().getStringExtra("discount_price");
        needPay=getIntent().getStringExtra("needPay");
        pay_type=getIntent().getStringExtra("pay_type");
        change=getIntent().getStringExtra("change");
        s_outtradeno_date=getIntent().getStringExtra("outtradeno_date");
        s_outtradeno=getIntent().getStringExtra("outtradeno");
        dikou=getIntent().getStringExtra("dikou");
        songbi= getIntent().getStringExtra("songbi");
        fangxian=getIntent().getStringExtra("fangxian");
        findView();
        initdata();
    }

    private void initdata() {
        order_no.setText(s_outtradeno);
        order_date.setText(s_outtradeno_date);
        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(XiaopiaoDialogActivity.this);
        String login_phone = preferences.getString("login_phone", "");
        if(!TextUtils.isEmpty(login_phone)){
        zhongduan.setText(login_phone);}
        store_name.setText(Stores.stores_name);
        List<GoodsListBean> goodsListBeanList=CashierActivity.goodsListBeanList;
        if(goodsListBeanList!=null&&goodsListBeanList.size()>0){
            XiaoPiaoAdapter adapter=new XiaoPiaoAdapter(this,goodsListBeanList);
            listview.setAdapter(adapter);
            listview.setDivider(null);
            Utils.Setlistview(listview);
        }
        listview_totalnum.setText(price_original);
        if(pay_type.equals(SettingData.PAYMENT_WAY_05+"Y")||pay_type.equals(SettingData.PAYMENT_WAY_05+"H")||pay_type.equals(SettingData.PAYMENT_WAY_05)) {
            if (!TextUtils.isEmpty(discount_price)) {
                xp_hyyh_jm.setText(discount_price );
            }
            if (!TextUtils.isEmpty(discount_price)) {
                xp_hyyh_fx.setText(MoneyUtil.formatMoney(fangxian));
            }
            if (!TextUtils.isEmpty(discount_price)) {
                xp_hyyh_sb.setText(songbi+"");
            }
        }
        if(!TextUtils.isEmpty(dikou)) {
            xp_qtdk.setText(MoneyUtil.formatMoney(dikou));

        }
        Log.v("zyl","支付方式为："+pay_type);
        Log.v("zyl","change："+change);
        if(SettingData.PAYMENT_WAY_04.equals(pay_type)) {
            try {
                if(change.equals("0.00")){
                    xp_sjfk.setText(MoneyUtil.moneySub(MoneyUtil.moneyAdd(price_original, change), dikou));
                }else{
                    xp_sjfk.setText(MoneyUtil.moneySub(MoneyUtil.moneyAdd(price_original, change), dikou) + " | 找零 " + change);
                }
            } catch (Exception e) {
                e.printStackTrace();
                xp_sjfk.setText(price_original);
            }
        }else if(pay_type.equals(SettingData.PAYMENT_WAY_05+"Y")||pay_type.equals(SettingData.PAYMENT_WAY_05+"H")||pay_type.equals(SettingData.PAYMENT_WAY_05)){
            xp_sjfk.setText(MoneyUtil.moneySub(needPay, dikou) );
        }else {
            xp_sjfk.setText(MoneyUtil.moneySub(price_original, dikou) );
        }
        if(pay_type.equals(SettingData.PAYMENT_WAY_05+"Y")||pay_type.equals(SettingData.PAYMENT_WAY_05+"H")||pay_type.equals(SettingData.PAYMENT_WAY_05)) {
            xp_sjsk_bottom.setText(MoneyUtil.moneySub(needPay, dikou));
        }else{
            xp_sjsk_bottom.setText(MoneyUtil.moneySub(price_original, dikou));
        }
        if(SettingData.PAYMENT_WAY_04.equals(pay_type)) {
            /** 现金 **/
            xp_zffs.setText("现金支付");
        }else if(SettingData.PAYMENT_WAY_11.equals(pay_type)) {
            /** 微信扫码支付 **/
            xp_zffs.setText("微信支付");
        }else if(SettingData.PAYMENT_WAY_14.equals(pay_type)) {
            /** 翼支付扫码支付 **/
            xp_zffs.setText("翼支付");
        }else if(SettingData.PAYMENT_WAY_25.equals(pay_type)) {
            /** 和包支付扫码支付 **/
            xp_zffs.setText("和包支付");
        }else if(pay_type.equals(SettingData.PAYMENT_WAY_05+"H")) {
            /** 扫描支付**/
            xp_zffs.setText("扫码支付");
        }else if(pay_type.equals(SettingData.PAYMENT_WAY_05+"Y")) {
            /** 手机验证码支付**/
            xp_zffs.setText("验证码支付");
        }else if(SettingData.PAYMENT_WAY_10.equals(pay_type)) {
            /** 支付宝扫码支付 **/
            xp_zffs.setText("支付宝支付");
        }else if(SettingData.PAYMENT_WAY_05.equals(pay_type)) {
            /** 亲情卡支付 **/
            xp_zffs.setText("亲情卡支付");
        }
        img_bottom_close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CashierActivity.goodsListBeanList.clear();
                CashierActivity.handler.sendEmptyMessage(1);
                Intent intent = new Intent();
                intent.setClass(XiaopiaoDialogActivity.this, SumNewActivity.class);
                setResult(Activity.RESULT_OK, intent);
                finish();
            }
        });

    }

    @Override
    public void onBackPressed() {

        CashierActivity.goodsListBeanList.clear();
        CashierActivity.handler.sendEmptyMessage(1);
        Intent intent = new Intent();
        intent.setClass(XiaopiaoDialogActivity.this, SumNewActivity.class);
        setResult(Activity.RESULT_OK, intent);
        super.onBackPressed();
        finish();
    }
    private void findView() {
        store_name= (TextView) findViewById(R.id.xp_title);
        order_no= (TextView) findViewById(R.id.xp_danhao);
        order_date= (TextView) findViewById(R.id.xp_shijian);
        zhongduan= (TextView) findViewById(R.id.xp_zhongduan);
        listview= (ListView) findViewById(R.id.xp_listview);
        listview_totalnum= (TextView) findViewById(R.id.xp_relat2_tv2);
        xp_hyyh_jm= (TextView) findViewById(R.id.xp_hyyh_jm);
        xp_hyyh_fx= (TextView) findViewById(R.id.xp_hyyh_fx);
        xp_hyyh_sb= (TextView) findViewById(R.id.xp_hyyh_sb);
        xp_qtdk=(TextView) findViewById(R.id.xp_qtdk);
        xp_psfy=(TextView) findViewById(R.id.xp_psfy);
        xp_zffs=(TextView) findViewById(R.id.xp_zffs);
        xp_sjfk=(TextView) findViewById(R.id.xp_qtdk_1);
        xp_gmcp= (TextView) findViewById(R.id.xp_gmcp);
        xp_sjsk_bottom=(TextView) findViewById(R.id.xp_relat3_tv2);
        lin_bottom_dy= (LinearLayout) findViewById(R.id.xp_bottom_lin1);
        lin_bottom_cp= (LinearLayout) findViewById(R.id.xp_bottom_lin2);
        lin_bottom_yhq= (LinearLayout) findViewById(R.id.xp_bottom_lin3);
        img_bottom_close= (ImageView) findViewById(R.id.xp_x);
    }
}
